<?php

/**
 * @author Vince Loremia
 * @version  1.0.0
 * @copyright 2008
 */

class GroupcontrolController extends Zend_Controller_Action
{
	public function init()
	{
		$this->initView();
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$this->view->webInfo = $webInfoNamespace->webInfo;
		//Zend_Loader::loadClass('HeaderLink');
		
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		
		$countWords = strlen($_SERVER['REQUEST_URI']);
		if (strpos($_SERVER['REQUEST_URI'], '/', $countWords-1) == true)
		{
			$this->_redirect(substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1));
			//$this->view->testingLang = substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1);
		}
		$this->view->user = Zend_Auth::getInstance()->getIdentity();
		# Links
		$arrayLinksNamespace = new Zend_Session_Namespace('arrayLinks');
		$arrayLinks = $arrayLinksNamespace->arrayLinksConfig;
		$this->view->links = $arrayLinks;
		# end of links
		Zend_Loader::loadClass('GroupProfile');
		Zend_Loader::loadClass('GroupJumper');
		Zend_Loader::loadClass('Category');
		Zend_Loader::loadClass('GroupAvatar');		
		Zend_Loader::loadClass('Access');
	}
	public function indexAction()
	{
		//Header
		$this->view->keywords = 'Edison, Quinones, Jr ';
		$this->view->description = 'This is the philippines international airport';
		$this->view->author = 'Edison P. Quinones Jr.';
		$this->view->robots = 'Index';
		$this->view->title = 'Groupjump. The World on the Web!';

		//Page Located
		$this->view->stringToCompareWithTheLink = 'Jumpsites';
		$this->view->PageContentTitle = 'leftLabel';
	}
	
	public function viewAction()
	{
		$filter = new Zend_Filter_StripTags();
		
		$id = trim($filter->filter($this->_request->getParam('id')));
		$this->view->id = $id;
		
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$webInfo = $webInfoNamespace->webInfo;
		
		$this->view->headerAdditional = '
   	
			<link rel="stylesheet" type="text/css" href="'.$webInfo.'extjs/resources/css/ext-all.css" />

    		<script type="text/javascript" src="'.$webInfo.'extjs/adapter/ext/ext-base.js"></script>
    		<script type="text/javascript" src="'.$webInfo.'extjs/ext-all-debug.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/group_control/ux/menu/EditableItem.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/group_control/ux/menu/RangeMenu.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/group_control/group_control.js"></script>
			
    		<link rel="stylesheet" type="text/css" href="'.$webInfo.'css/grid-examples.css" />
			<link rel="stylesheet" type="text/css" href="'.$webInfo.'css/forms.css"/>
			';
		//$jumpsiteDisplay = new JumpsiteDisplay();
		//$this->view->jumpsite = $jumpsiteDisplay->getGroupToDisplay($jumpsiteId);		
	}
	public function showgroupAction()
	{	
		$filter = new Zend_Filter_StripTags();		
		$id = trim($filter->filter($this->_request->getParam('id')));	
		$group = new GroupJumper(); 
		$result = $group->showGroups($id);
		$json   = Zend_Json::encode($result);
		$this->view->group = $json;
	}
	public function getcategoryAction()
	{		
		$category = new Category();
	
		$result    = $category->fetchAll();
		$json      = Zend_Json::encode($result->toArray());
		$this->view->category = $json;
	}
	public function getavatarAction()
	{		
		$avatar = new GroupAvatar();
	
		$result = $avatar->fetchAll();
		$json   = Zend_Json::encode($result->toArray());
		$this->view->avatar = $json;
	}
	public function updateAction()
	{
		$group = new GroupProfile();
		
	   	$filter = new Zend_Filter_StripTags();		
		$value 	= trim($filter->filter($this->_request->getPost('value')));
		$field 	= trim($filter->filter($this->_request->getPost('field')));
		$keyID 	= trim($filter->filter($this->_request->getPost('keyID')));
		$id 	= trim($filter->filter($this->_request->getParam('id'))); 
		if($field == 'category_name') 
		  	$field = 'category_id';
		$newRecord = $keyID == 0 ? 'yes' : 'no'; 
		$data = array(
					$field => $value
				);
		$where = 'id ='.$id;
		
		$n = $group->update($data, $where);
		
    	if ($n > 0) {
        	
            	$this->view->success = "{success:true}";
        
    	} else {
        	$this->view->success = "{success:false}"; //if we want to trigger the false block we should redirect somewhere to get a 404 page
    	}
	}
	public function removeAction()
	{
		$group = new GroupProfile();
    	$count = 0;
    	$filter = new Zend_Filter_StripTags();		
		$id 	= trim($filter->filter($this->_request->getPost('id')));
		$where  = "id = $id";
		$n = $group->delete($where);
       
    	if ($n > 0) { 
    		$count = 1;
        	$cb = isset($_GET['callback']) ? $_GET['callback'] : '';           
        	$response = array('success'=>$count, 'del_count'=>$count);			
			$json   = Zend_Json::encode($response);        
        	$this->view->message = $cb . $json;
        	
    	} else {
        	$this->view->message = "{failure: true}";
    	}
		
	}
	
}